package StudentProject.dao;

import StudentProject.jdbc.JDBCUtils;

import java.sql.*;
import java.util.Scanner;

public class GradesDaoImp implements GradesDao {


    // 添加成绩
    public  void addGrade(Scanner scanner) {
        System.out.println("请输入学号：");
        int studentId = scanner.nextInt();
        System.out.println("请输入课程代码：");
        String courseCode = scanner.next();
        System.out.println("请输入成绩：");
        double score = scanner.nextDouble();

        try (Connection connection = JDBCUtils.getConnection();
             PreparedStatement statement = connection.prepareStatement("INSERT INTO grades (student_id, course_code, score) VALUES (?,?,?)")) {

            statement.setInt(1, studentId);
            statement.setString(2, courseCode);
            statement.setDouble(3, score);

            statement.executeUpdate();

            System.out.println("成绩添加成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 删除成绩
    public  void deleteGrade(Scanner scanner) {
        System.out.println("请输入要删除的成绩 ID：");
        int gradeId = scanner.nextInt();

        try (Connection connection = JDBCUtils.getConnection();
             PreparedStatement statement = connection.prepareStatement("DELETE FROM grades WHERE grade_id =?")) {

            statement.setInt(1, gradeId);

            statement.executeUpdate();

            System.out.println("成绩删除成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 修改成绩
    public  void updateGrade(Scanner scanner) {
        System.out.println("请输入要修改的成绩 ID：");
        int gradeId = scanner.nextInt();
        System.out.println("请输入新的成绩：");
        double newScore = scanner.nextDouble();

        try (Connection connection = JDBCUtils.getConnection();
             PreparedStatement statement = connection.prepareStatement("UPDATE grades SET score =? WHERE grade_id =? ")) {

            statement.setDouble(1, newScore);
            statement.setInt(2, gradeId);

            statement.executeUpdate();

            System.out.println("成绩修改成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 查询成绩
    public  void queryGrade(Scanner scanner) {
        System.out.println("请选择查询方式：");
        System.out.println("1. 按成绩 ID 查询");
        System.out.println("2. 按学号查询");
        System.out.println("3. 按课程代码查询");

        int queryChoice = scanner.nextInt();
        PreparedStatement statement = null;
        try (Connection connection = JDBCUtils.getConnection()) {

            if (queryChoice == 1) {
                System.out.println("请输入成绩 ID：");
                int gradeId = scanner.nextInt();

                statement = connection.prepareStatement("SELECT * FROM grades WHERE grade_id =?");
                statement.setInt(1, gradeId);
            } else if (queryChoice == 2) {
                System.out.println("请输入学号：");
                int studentId = scanner.nextInt();

                statement = connection.prepareStatement("SELECT * FROM grades WHERE student_id =?");
                statement.setInt(1, studentId);
            } else if (queryChoice == 3) {
                System.out.println("请输入课程代码：");
                String courseCode = scanner.next();

                statement = connection.prepareStatement("SELECT * FROM grades WHERE course_code =?");
                statement.setString(1, courseCode);
            }

            ResultSet resultSet = statement.executeQuery();

            while (resultSet.next()) {
                int gradeId = resultSet.getInt("grade_id");
                int studentId = resultSet.getInt("student_id");
                String courseCode = resultSet.getString("course_code");
                double score = resultSet.getDouble("score");

                System.out.println("成绩 ID：" + gradeId + "，学号：" + studentId + "，课程代码：" + courseCode + "，成绩：" + score);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}